package jinyilw.tools.h2db;

import jinyilw.common.app.AppConfig;
import jinyilw.common.SettingTools;
import jinyilw.common.TextTools;
import jinyilw.common.database.DBParameter;
import jinyilw.common.database.JdbcTools;
import org.h2.tools.Server;

import java.io.File;

public class DBConfig
{
	//	public static final String defaultUsername = "root";
	//	public static final String defaultPassword = "12345678";
	//	public static final String defaultDriver = "com.mysql.jdbc.Driver";
	//	public static final String defaultUrl =
	//	"jdbc:mysql://localhost:3306/sms?useUnicode=true&characterEncoding=utf-8";
	public static String dbSettingsFileName = "Database.ini";
	private static volatile SettingTools dbSettings;

	private static boolean isH2Server = false;
	private static String h2ServerIP = "localhost";
	private static String replaceIP;
	private static int h2Port = 9080;
	private static String h2Path = "~/.database/test";
	private static String driver = "org.h2.Driver";
	private static String url = "jdbc:h2:tcp://" + h2ServerIP + ":" + h2Port + "/"
			+ h2Path;
	private static String username = "root";
	private static String password = "";
	private static Server h2Server;
	private static boolean needInitial = false;

	private static volatile JdbcTools defaultTools;
	private static DBParameter defaultDBParameter;

	private DBConfig()
	{
	}

	public static void set(String dbSettingsFileName, boolean isH2Server, String h2ServerIP,
			int h2Port,
			String h2Path, String username, String password)
	{
		DBConfig.dbSettingsFileName = dbSettingsFileName;
		DBConfig.isH2Server = isH2Server;
		DBConfig.h2ServerIP = h2ServerIP;
		DBConfig.h2Port = h2Port;
		DBConfig.h2Path = h2Path;
		DBConfig.username = username;
		DBConfig.password = password;
		url = "jdbc:h2:tcp://" + h2ServerIP + ":" + h2Port + "/"
				+ h2Path;
	}

	private synchronized static void initSettings()
	{
		if (dbSettings == null)
		{
			File settingsFile = new File(AppConfig.getSettingsFolder(),
					dbSettingsFileName);
			dbSettings = SettingTools.getTools(settingsFile);

			h2ServerIP = dbSettings.getString("h2dbServerIP", h2ServerIP);
			if (replaceIP != null)
				h2ServerIP = replaceIP;

			driver = dbSettings.getString("driver", driver);
			url = dbSettings.getString("url", url);
			username = dbSettings.getString("username", username);
			password = dbSettings.getString("password", password);
			isH2Server = dbSettings.getBool("isH2dbServer", isH2Server);
			needInitial = dbSettings.getBool("needInitial", needInitial);
			h2Path = dbSettings.getString("h2dbPath", h2Path);
			h2Port = dbSettings.getInt("h2dbPort", h2Port);

			dbSettings.putData("driver", driver);
			dbSettings.putData("url", url);
			dbSettings.putData("username", username);
			dbSettings.putData("password", password);
			dbSettings.putData("h2dbServerIP", h2ServerIP);
			dbSettings.putData("isH2dbServer", isH2Server);
			dbSettings.putData("h2dbPath", h2Path);
			dbSettings.putData("h2dbPort", h2Port);
		}
	}

	public static String getH2ServerIP()
	{
		initSettings();
		return h2ServerIP;
	}

	public static String getUrl()
	{
		initSettings();
		return url;
	}

	public static boolean needInitial()
	{
		initSettings();
		return needInitial;
	}

	public static void setNeedInitial(boolean b)
	{
		if (needInitial != b)
		{
			needInitial = b;
			initSettings();
			dbSettings.putData("needInitial", b);
		}
	}

	public static DBParameter getDefaultDBParameter()
	{
		initSettings();
		if (defaultDBParameter == null)
			defaultDBParameter = new DBParameter(driver, url, username,
					password);
		if (isH2Server && h2Server == null)
		{
			File dbFile = H2dbTools.getFile(h2Path);
			needInitial = !dbFile.isFile();
			h2Server = H2dbTools.startH2TcpServer(h2Port, h2Path, username,
					password);
			defaultDBParameter.driver = H2dbTools.h2Driver;
			defaultDBParameter.url = "jdbc:h2:" + h2Path;
		}
		return defaultDBParameter;
	}

	public static boolean setServerIP(String newIP)
	{
		if (TextTools.isBlank(newIP))
			return false;
		replaceIP = newIP;
		url = url.replace(h2ServerIP, newIP);
		h2ServerIP = newIP;
		if (dbSettings != null)
		{
			dbSettings.putData("serverIP", h2ServerIP);
			dbSettings.putData("url", url);
		}
		if (defaultDBParameter != null)
			defaultDBParameter.url = url;
		return true;
	}

	public static void getClientSettings()
	{

	}

	public static JdbcTools getDefaultTools()
	{
		if (defaultTools == null)
			synchronized (DBConfig.class)
			{
				if (defaultTools == null)
					defaultTools = new JdbcTools(
							DBConfig.getDefaultDBParameter());
			}
		return defaultTools;
	}
}
